﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.TypeTree
{
    public partial class GenerateHtml : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);


            string kind = Request["kind"];
            if (!string.IsNullOrEmpty(kind))
            {    string htmlStr = "";
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (kind == "PcMainNav")
                  {
                          CmsTreeDef rtreeDef = session.CreateQuery("from CmsTreeDef where Kind='PcMainNav' and Parent='0' ").UniqueResult<CmsTreeDef>();
                          IList<CmsTreeDef> treeDefList = session.CreateQuery("from CmsTreeDef where Kind='PcMainNav' and Parent='" + rtreeDef.Id + "' order by ShowOrder  ").List<CmsTreeDef>();
                          foreach (CmsTreeDef t in treeDefList)
                          {
                              htmlStr += " <li><a href='" + t.Value + "' >" + t.Name + "</a></li>";
                          }
                  }
                    else if (kind == "PcProductNav")
                    {
                        CmsTreeDef rtreeDef = session.CreateQuery("from CmsTreeDef where Kind='PcProductNav' and Parent='0' ").UniqueResult<CmsTreeDef>();
                        IList<CmsTreeDef> ftreeDefList = session.CreateQuery("from CmsTreeDef where Kind='PcProductNav' and Parent='" + rtreeDef.Id + "' order by ShowOrder  ").List<CmsTreeDef>();
                        for (int j = 0; j < ftreeDefList.Count; j++)
                        {
                            htmlStr += "<li style=\"background: url('" + ftreeDefList[j].Param + "') no-repeat scroll 5px center " + (((j + 1) % 2) == 0 ? "#f5f5f5" : "rgba(0, 0, 0, 0)") + ";\" class=''><a href='" + ftreeDefList[j].Value + "' >" + ftreeDefList[j].Name + "</a><div class='wrap'>";
                            IList<CmsTreeDef> treeDefList = session.CreateQuery("from CmsTreeDef where Kind='PcProductNav' and Parent='" + ftreeDefList[j].Id + "'  order by ShowOrder ").List<CmsTreeDef>();
                            foreach (CmsTreeDef t in treeDefList)
                            {
                                htmlStr += "<p><a href='" + t.Value + "'><span>" + t.Name + "</span></a></p> <ul class='wrap_list'>";
                                IList<CmsTreeDef> streeDefList = session.CreateQuery("from CmsTreeDef where Kind='PcProductNav' and Parent='" + t.Id + "'  order by ShowOrder ").List<CmsTreeDef>();
                                for (int i = 0; i < streeDefList.Count; i++)
                                {

                                    htmlStr += "<li ";
                                    if (i == 0)
                                    {
                                        htmlStr += " class='fir'";
                                    }
                                    htmlStr += "><a href='" + streeDefList[i].Value + "'>" + streeDefList[i].Name + "</a></li>";
                                }
                                htmlStr += "<div class='clear'></div></ul>";
                            }
                            htmlStr += "  </div></li>";
                        }
                        
                    }
                    else if (kind == "FooterNav")
                    {
                        CmsTreeDef rtreeDef = session.CreateQuery("from CmsTreeDef where Kind='FooterNav' and Parent='0' ").UniqueResult<CmsTreeDef>();
                        IList<CmsTreeDef> ftreeDefList = session.CreateQuery("from CmsTreeDef where Kind='FooterNav' and Parent='" + rtreeDef.Id + "' order by ShowOrder  ").List<CmsTreeDef>();
                        foreach (CmsTreeDef ft in ftreeDefList)
                        {
                            htmlStr += " <div class='site-info__item'><h3>" + ft.Name + "</h3><ul>";
                            IList<CmsTreeDef> treeDefList = session.CreateQuery("from CmsTreeDef where Kind='FooterNav' and Parent='" + ft.Id + "'  order by ShowOrder ").List<CmsTreeDef>();
                            foreach (CmsTreeDef t in treeDefList)
                            {
                                htmlStr += "  <li><a target='_blank' href='" + t.Value + "' rel='nofollow'>" + t.Name + "</a></li>";
                            }
                            htmlStr += "  </ul> </div>";
                        }
                    }
                    session.CreateQuery("update CmsModuleData set Content=:content where Id=:id").SetParameter("content", htmlStr).SetParameter("id", kind).ExecuteUpdate();
                }
                _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!" });
            }
            else
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-参数错误!" });
            }

        }
    }
}